from datetime import datetime
from django.db import models


class Patient(models.Model):
    """就诊人信息"""
    sex_choice = [
        (1, '男'),
        (2, '女'),
    ]
    name = models.CharField(max_length=200, verbose_name='姓名',null=True)
    phone = models.CharField(max_length=11, verbose_name='就诊人手机',null=True)
    age = models.IntegerField(verbose_name='年龄', null=True)
    sex = models.CharField(max_length=10, verbose_name='就诊人性别', choices=sex_choice,null=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'Patient'


class User(models.Model):
    """用户信息"""
    phone = models.CharField(max_length=20, verbose_name='手机号码', null=True)
    gesture_pwd = models.CharField(max_length=200, verbose_name='手势密码', null=True, blank=True)
    common_pwd = models.CharField(max_length=200, verbose_name='普通密码', null=True)
    p_profile_picture = models.CharField(max_length=200, verbose_name='头像', default='https://n.sinaimg.cn/sinakd10101/432/w649h583/20200405/881c-irtymmw1298316.jpg')
    patient = models.ForeignKey(Patient, on_delete=models.CASCADE, verbose_name='就诊人信息管理id', null=True)
    address_message = models.CharField(max_length=200, verbose_name='地址', null=True)
    nickname = models.CharField(max_length=200, verbose_name='昵称', null=True)

    def __str__(self):
        return self.phone

    class Meta:
        verbose_name = 'User'


class Hospital(models.Model):
    """"医院信息"""
    level_choice =[
        (1, "一甲医院"),
        (2, "二甲医院"),
        (3, "三甲医院"),
    ]
    class_choice = [
        (1, "综合医院"),
        (2, "专科医院"),
    ]
    h_name = models.CharField(max_length=120, verbose_name='医院名称')
    hospital_level = models.CharField(max_length=120, verbose_name='医院等级', choices=level_choice)
    hospital_classification = models.CharField(max_length=120, verbose_name='医院类别', choices=class_choice)
    address = models.CharField(max_length=200,verbose_name='地址',null=True)
    img = models.CharField(max_length=200, verbose_name='医院图片',null=True)

    def __str__(self):
        return self.h_name

    class Meta:
        verbose_name = 'Hospital'

    def tojson(self):
        return {
            'id': self.id,
            'name': self.h_name,
            'level': self.hospital_level,
            'type': self.hospital_classification,
            "img": self.img,
            "address": self.address,
        }


class Department(models.Model):
    """"科室信息"""
    professional_department = models.CharField(max_length=200, verbose_name='专业科室')
    hospital = models.ForeignKey(Hospital, on_delete=models.CASCADE, verbose_name='医院id', null=True)

    def __str__(self):
        return self.professional_department

    class Meta:
        verbose_name = 'Department'


class Doctor(models.Model):
    """医生信息"""
    positional_choice = [
        (1, '主任'),
        (2, '副主任'),
        (3, '主治医师'),
    ]
    d_name = models.CharField(max_length=120, verbose_name='医生姓名', null=True)
    d_age = models.IntegerField(verbose_name='医生年龄', null=True)
    medical_years = models.IntegerField(verbose_name='医龄', null=True)
    positional_titles = models.CharField(max_length=200, verbose_name='医生职称', choices=positional_choice, null=True)
    department = models.ForeignKey(Department, on_delete=models.CASCADE, verbose_name='科室id', null=True)

    def __str__(self):
        return self.d_name

    class Meta:
        verbose_name = 'Doctor'


class EscortServiceItem(models.Model):
    """陪诊师服务项目"""
    type_choice = [
        (1, '院内陪护'),
        (2, '就医陪护'),
    ]
    escort_type = models.CharField(max_length=120, verbose_name='陪护类型', choices=type_choice)
    service_item = models.CharField(max_length=120, verbose_name='服务项目')
    service_price = models.FloatField(verbose_name='价格')
    service_introduction = models.TextField(verbose_name='服务说明', default=None)

    def __str__(self):
        return self.service_item

    class Meta:
        verbose_name = 'EscortServiceItem'

    def tojson(self):
        return {
            'service_item': self.service_item,
            'service_price': self.service_price,
            'service_introduction': self.service_introduction
        }


class HospitalEscort(models.Model):
    """陪诊师信息"""
    e_name = models.CharField(max_length=120,verbose_name='陪诊师姓名', null=True)
    e_password = models.CharField(max_length=120,verbose_name='陪诊师密码', null=True)
    e_card = models.CharField(max_length=120,verbose_name='身份证号', null=True)
    e_phone = models.CharField(max_length=11, verbose_name='陪诊师手机号')
    e_age = models.IntegerField(verbose_name='陪诊师年龄', null=True)
    e_sex = models.CharField(max_length=10, verbose_name='陪诊师性别', null=True)
    address = models.CharField(max_length=120, verbose_name='地址', null=True)
    favorable_rate = models.FloatField(verbose_name='评分', null=True)
    order_count = models.IntegerField(verbose_name='接单量', null=True)
    service_items = models.ManyToManyField(EscortServiceItem, verbose_name='提供的服务项目', related_name='acc')
    status = models.CharField(max_length=10, verbose_name='陪诊师状态', default='空闲')
    img = models.CharField(max_length=210, verbose_name='头像', null=True)
    occupancy = models.CharField(max_length=20, verbose_name='入驻', default='未入驻')

    def __str__(self):
        return self.e_name

    class Meta:
        verbose_name = 'HospitalEscort'

    def tojson(self):
        return {
            'e_name': self.e_name,
            'e_phone': self.e_phone,
            'e_age': self.e_age,
            'e_sex': self.e_sex,
            'address': self.address,
            'favorable_rate': self.favorable_rate,
            'order_count': self.order_count,
            'status': self.status,
            'img': self.img,
            'occupancy': self.occupancy
        }

class Orders(models.Model):
    """基类表"""
    service_item = models.CharField(max_length=50, verbose_name='订单名称', null=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户id')
    hospital = models.CharField(max_length=200, verbose_name='医院id')
    escort = models.CharField(max_length=50, verbose_name='陪诊师')
    patient = models.CharField(max_length=50, verbose_name='患者')
    coupon = models.CharField(max_length=50, verbose_name='优惠券', null=True)
    data_upload = models.TextField(verbose_name='资料（病例）可选', null=True)
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格')
    status = models.CharField(max_length=10, verbose_name='状态', default='未支付')
    receive_info = models.TextField(verbose_name='收件信息', null=True)
    report_info = models.TextField(verbose_name='报告信息',  null=True)
    department = models.CharField(max_length=50, verbose_name='科室', null=True)
    doctor = models.CharField(max_length=50, verbose_name='医生', null=True)
    expect_time = models.DateTimeField(verbose_name='期望处理时间', null=True)
    medicine_name = models.CharField(max_length=200, verbose_name='药品名称', null=True)
    medicine_type = models.CharField(max_length=50, verbose_name='药物类型', null=True)
    delivery_address = models.CharField(max_length=200, verbose_name='送货地址', null=True)
    evaluation = models.CharField(max_length=200, verbose_name='评价', null=True)
    score = models.FloatField(verbose_name='评分', default=5)

    class Meta:
        verbose_name = 'Orders'

    def tojson(self):
        return {
            "id": self.id,
            "price": self.price,
            "service_item": self.service_item,
            "hospital": self.hospital,
            "escort": self.escort,
            "patient": self.patient,
            "status": self.status,
            "expect_time": self.expect_time.strftime('%Y-%m-%d') if self.expect_time else None,
            "data_upload": self.data_upload,
            'evaluation': self.evaluation,
            'score': self.score
        }

